﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.Paint.PaintDuringSimulation.CApPaintDuringSimulationManager
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.Paint.DisplayCoverage;
using EngineeringInternalExtension;
using EngineeringInternalExtension.DataTypes.Paint;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.Paint.PaintDuringSimulation;

public class CApPaintDuringSimulationManager
{
  private bool m_paintDuringSimulation;
  private CApPaintDuringSimulationManager.SimulationState m_paintSimulation;
  private bool m_registeredForSimulationEvents;
  private Dictionary<TxPaintGun, CApPaintGun> m_paintGunData = new Dictionary<TxPaintGun, CApPaintGun>();
  private bool m_displayBrush;
  private bool m_initialized;
  private static CApPaintDuringSimulationManager instance;

  private CApPaintDuringSimulationManager()
  {
    this.IsDocumentLoaded = TxApplication.ActiveDocument != null && TxApplication.ActiveDocument.PlatformGlobalServicesProvider.IsDocumentLoaded();
    if (this.IsDocumentLoaded)
      return;
    this.RegisterToDocumentEvents();
  }

  public static CApPaintDuringSimulationManager Instance
  {
    get
    {
      if (CApPaintDuringSimulationManager.instance == null)
        CApPaintDuringSimulationManager.instance = new CApPaintDuringSimulationManager();
      return CApPaintDuringSimulationManager.instance;
    }
  }

  public bool UsePaintThickness
  {
    get => CApPaintDisplayCoverageManager.Instance.DisplayThicknessValue;
    set => CApPaintDisplayCoverageManager.Instance.DisplayThicknessValue = value;
  }

  public bool PaintDuringSimulation
  {
    get => this.m_paintDuringSimulation;
    set
    {
      this.m_paintDuringSimulation = value;
      this.SetPaintSetings(this.m_paintDuringSimulation);
      this.RegisterEvents(this.m_paintDuringSimulation);
    }
  }

  internal bool IsDocumentLoaded { private set; get; }

  private void SetPaintSetings(bool flag)
  {
    TxPaintSettings paintSettings = TxApplicationEx.PaintManager().PaintSettings;
    paintSettings.PaintDuringSimulation = flag;
    TxApplicationEx.PaintManager().PaintSettings = paintSettings;
  }

  private void RegisterEvents(bool paintDuringSimuation)
  {
    if (paintDuringSimuation)
      this.RegisterSimulationEvents();
    else
      this.UnRegisterSimulationEvents();
  }

  public void RegisterSimulationEvents()
  {
    try
    {
      TxDocument activeDocument = TxApplication.ActiveDocument;
      if (activeDocument == null || this.m_registeredForSimulationEvents)
        return;
      this.m_registeredForSimulationEvents = true;
      activeDocument.SimulationPlayer.SignificantTimeReached += new TxSimulationPlayer_SignificantTimeReachedEventHandler(this.onSignificantTimeReached);
      activeDocument.SimulationPlayer.SimulationCleared += new TxSimulationPlayer_SimulationClearedEventHandler(this.onSimulationCleared);
      activeDocument.SimulationPlayer.SimulationStartedForward += new TxSimulationPlayer_SimulationStartedForwardEventHandler(this.onSimulationStartedForward);
      activeDocument.SimulationPlayer.SimulationStopped += new TxSimulationPlayer_SimulationStoppedEventHandler(this.onSimulationStopped);
      activeDocument.SimulationPlayer.SimulationEndedForward += new TxSimulationPlayer_SimulationEndedForwardEventHandler(this.onSimulationEndedForward);
      activeDocument.SimulationPlayer.SimulationStartedBackward += new TxSimulationPlayer_SimulationStartedBackwardEventHandler(this.onSimulationStartedBackward);
      activeDocument.SimulationPlayer.SimulationEndedBackward += new TxSimulationPlayer_SimulationEndedBackwardEventHandler(this.onSimulationEndedBackward);
    }
    catch (Exception ex)
    {
    }
  }

  public void UnRegisterSimulationEvents()
  {
    try
    {
      TxDocument activeDocument = TxApplication.ActiveDocument;
      if (activeDocument == null || !this.m_registeredForSimulationEvents)
        return;
      this.m_registeredForSimulationEvents = false;
      activeDocument.SimulationPlayer.SignificantTimeReached -= new TxSimulationPlayer_SignificantTimeReachedEventHandler(this.onSignificantTimeReached);
      activeDocument.SimulationPlayer.SimulationCleared -= new TxSimulationPlayer_SimulationClearedEventHandler(this.onSimulationCleared);
      activeDocument.SimulationPlayer.SimulationStartedForward -= new TxSimulationPlayer_SimulationStartedForwardEventHandler(this.onSimulationStartedForward);
      activeDocument.SimulationPlayer.SimulationStopped -= new TxSimulationPlayer_SimulationStoppedEventHandler(this.onSimulationStopped);
      activeDocument.SimulationPlayer.SimulationEndedForward -= new TxSimulationPlayer_SimulationEndedForwardEventHandler(this.onSimulationEndedForward);
      activeDocument.SimulationPlayer.SimulationStartedBackward -= new TxSimulationPlayer_SimulationStartedBackwardEventHandler(this.onSimulationStartedBackward);
      activeDocument.SimulationPlayer.SimulationEndedBackward -= new TxSimulationPlayer_SimulationEndedBackwardEventHandler(this.onSimulationEndedBackward);
    }
    catch (Exception ex)
    {
    }
  }

  private void onSignificantTimeReached(
    object sender,
    TxSimulationPlayer_SignificantTimeReachedEventArgs args)
  {
    if (this.m_paintSimulation != CApPaintDuringSimulationManager.SimulationState.SimulationForward)
      return;
    this.VerifyPaintGunIsOpenAndBrushNotDefined();
    if (this.IsAtLeastOnePaintGunPainting())
    {
      TxObjectList<Tx2Or3DimensionalGeometry> ofDisplayedParts = CUiPaintDisplayCoverageUtil.GetMeshEntitiesOfDisplayedParts();
      TxApplicationEx.PaintManager().SetMeshList(ofDisplayedParts);
    }
    this.ApplyCoverage(TxApplication.ActiveDocument.SimulationPlayer.CurrentTime);
  }

  private void onSimulationCleared(object sender, TxSimulationPlayer_SimulationClearedEventArgs e)
  {
    this.ClearUsingPaintSimulation();
    this.m_paintSimulation = CApPaintDuringSimulationManager.SimulationState.NoSimulation;
    CUiPaintDisplayCoverageUtil.UpdateMeshVisibility();
  }

  private void onSimulationStartedForward(
    object sender,
    TxSimulationPlayer_SimulationStartedForwardEventArgs args)
  {
    CApPaintDisplayCoverageManager.Instance.LoadPaintSettingsForSimulatation();
    CUiPaintDisplayCoverageUtil.DisplayAllMeshes();
    this.m_displayBrush = CApPaintDisplayCoverageManager.Instance.DisplayBrush;
    if (this.m_paintSimulation == CApPaintDuringSimulationManager.SimulationState.NoSimulation)
    {
      TxApplicationEx.PaintManager().CloseResult();
      this.m_paintSimulation = CApPaintDuringSimulationManager.SimulationState.SimulationForward;
      this.ClearUsingPaintSimulation();
      this.FindAllBrushesAndRegister();
    }
    else
    {
      this.m_paintSimulation = CApPaintDuringSimulationManager.SimulationState.SimulationForward;
      this.ContinueUsingPaintSimulation();
    }
  }

  private void onSimulationEndedForward(
    object sender,
    TxSimulationPlayer_SimulationEndedForwardEventArgs args)
  {
    this.ClearUsingPaintSimulation();
    this.m_paintSimulation = CApPaintDuringSimulationManager.SimulationState.NoSimulation;
    CUiPaintDisplayCoverageUtil.UpdateMeshVisibility();
  }

  private void onSimulationStopped(
    object sender,
    TxSimulationPlayer_SimulationStoppedEventArgs args)
  {
    if (this.m_paintSimulation == CApPaintDuringSimulationManager.SimulationState.SimulationForward)
    {
      this.m_paintSimulation = CApPaintDuringSimulationManager.SimulationState.SimulationStopForward;
      this.StopUsingPaintSimulation();
      CUiPaintDisplayCoverageUtil.UpdateMeshVisibility();
    }
    else
    {
      if (this.m_paintSimulation != CApPaintDuringSimulationManager.SimulationState.SimulationBackward)
        return;
      this.m_paintSimulation = CApPaintDuringSimulationManager.SimulationState.SimulationStopBackward;
      this.StopUsingPaintSimulation();
    }
  }

  private void onSimulationStartedBackward(
    object sender,
    TxSimulationPlayer_SimulationStartedBackwardEventArgs args)
  {
    if (this.m_paintSimulation == CApPaintDuringSimulationManager.SimulationState.NoSimulation)
    {
      this.m_paintSimulation = CApPaintDuringSimulationManager.SimulationState.SimulationBackward;
      this.ClearUsingPaintSimulation();
      this.FindAllBrushesAndRegister();
    }
    else
    {
      this.m_paintSimulation = CApPaintDuringSimulationManager.SimulationState.SimulationBackward;
      this.ContinueUsingPaintSimulation();
    }
  }

  private void onSimulationEndedBackward(
    object sender,
    TxSimulationPlayer_SimulationEndedBackwardEventArgs args)
  {
    this.ClearUsingPaintSimulation();
    this.m_paintSimulation = CApPaintDuringSimulationManager.SimulationState.NoSimulation;
  }

  private void RegisterPaintGun(ITxPaintGun paintGun)
  {
    paintGun.Opened += new TxPaintGun_OpenedEventHandler(this.paintGun_Opened);
    paintGun.Closed += new TxPaintGun_ClosedEventHandler(this.paintGun_Closed);
    paintGun.BrushChanged += new TxPaintGun_BrushChangedEventHandler(this.paintGun_BrushChanged);
  }

  private void UnRegisterPaintGun(ITxPaintGun paintGun)
  {
    paintGun.Opened -= new TxPaintGun_OpenedEventHandler(this.paintGun_Opened);
    paintGun.Closed -= new TxPaintGun_ClosedEventHandler(this.paintGun_Closed);
    paintGun.BrushChanged -= new TxPaintGun_BrushChangedEventHandler(this.paintGun_BrushChanged);
  }

  private void paintGun_Closed(object sender, TxPaintGun_ClosedEventArgs args)
  {
    if (!(sender is TxPaintGun key) || !this.m_paintGunData.ContainsKey(key))
      return;
    this.m_paintGunData[key].DisplayBrush(false);
    this.m_paintGunData[key].PaintGunState = CApPaintGun.GunState.Close;
  }

  private void paintGun_Opened(object sender, TxPaintGun_OpenedEventArgs args)
  {
    if (!(sender is TxPaintGun txPaintGun) || !this.m_paintGunData.ContainsKey(txPaintGun))
      return;
    this.m_paintGunData[txPaintGun].PaintGunState = CApPaintGun.GunState.FirstNode;
    if (!this.m_paintGunData[txPaintGun].ShouldPaint)
      return;
    this.PaintGunManipulatorDisplay(txPaintGun, false);
    this.m_paintGunData[txPaintGun].DisplayBrush(this.m_displayBrush);
  }

  private void paintGun_BrushChanged(object sender, TxPaintGun_BrushChangedEventArgs args)
  {
    if (!(sender is TxPaintGun txPaintGun) || !this.m_paintGunData.ContainsKey(txPaintGun) || args.BrushName == null)
      return;
    this.ChangePaintBrush(this.m_paintGunData[txPaintGun], txPaintGun, args.BrushName);
  }

  internal void RegisterToDocumentEvents()
  {
    TxApplication.DocumentCollection.DocumentLoaded += new TxDocumentCollection_DocumentLoadedEventHandler(this.OnDocumentLoaded);
  }

  internal void UnregisterFromDocumentEvents()
  {
    TxApplication.DocumentCollection.DocumentLoaded -= new TxDocumentCollection_DocumentLoadedEventHandler(this.OnDocumentLoaded);
  }

  private void OnDocumentLoaded(object sender, TxDocumentCollection_DocumentLoadedEventArgs args)
  {
    this.IsDocumentLoaded = true;
    this.UnregisterFromDocumentEvents();
  }

  private bool IsAtLeastOnePaintGunPainting()
  {
    bool flag = false;
    foreach (KeyValuePair<TxPaintGun, CApPaintGun> keyValuePair in this.m_paintGunData)
    {
      if (keyValuePair.Value.ShouldPaint)
      {
        flag = true;
        break;
      }
    }
    return flag;
  }

  private void VerifyPaintGunIsOpenAndBrushNotDefined()
  {
    foreach (KeyValuePair<TxPaintGun, CApPaintGun> keyValuePair in this.m_paintGunData)
    {
      if (keyValuePair.Value.PaintGunIsOpen && keyValuePair.Value.BrushId == null && !keyValuePair.Value.PaintGunIsOpenAndBrushNotDefinedNotification)
      {
        keyValuePair.Value.PaintGunIsOpenAndBrushNotDefinedNotification = true;
        this.RaiseErrorMessageGunOpenNoBrush(keyValuePair.Key);
      }
    }
  }

  private void ApplyCoverage(double currentTime)
  {
    foreach (KeyValuePair<TxPaintGun, CApPaintGun> keyValuePair in this.m_paintGunData)
    {
      if (keyValuePair.Value.ShouldPaint)
      {
        bool flag = false;
        if (keyValuePair.Value.PaintGunState == CApPaintGun.GunState.FirstNode)
        {
          keyValuePair.Value.PaintGunState = CApPaintGun.GunState.Open;
          flag = true;
        }
        TxApplicationEx.PaintManager().ApplyCoverage(keyValuePair.Value.BrushId, keyValuePair.Key.GunTip, currentTime, flag);
      }
    }
  }

  private void FindAllBrushesAndRegister()
  {
    foreach (TxPaintGun allDescendant in (Collection<ITxObject>) TxApplication.ActiveDocument.PhysicalRoot.GetAllDescendants((ITxTypeFilter) new TxTypeFilter(typeof (TxPaintGun))))
    {
      CApPaintGun capPaintGun = new CApPaintGun();
      this.m_paintGunData.Add(allDescendant, capPaintGun);
      this.RegisterPaintGun((ITxPaintGun) allDescendant);
      TxDisplayableObjectEx.SetSelectable((ITxDisplayableObject) allDescendant, false);
    }
  }

  private void StopUsingPaintSimulation()
  {
    foreach (KeyValuePair<TxPaintGun, CApPaintGun> keyValuePair in this.m_paintGunData)
    {
      this.UnRegisterPaintGun((ITxPaintGun) keyValuePair.Key);
      TxDisplayableObjectEx.SetSelectable((ITxDisplayableObject) keyValuePair.Key, true);
      if (keyValuePair.Value.ShouldPaint)
        keyValuePair.Value.DisplayBrush(false);
    }
  }

  private void ContinueUsingPaintSimulation()
  {
    foreach (KeyValuePair<TxPaintGun, CApPaintGun> keyValuePair in this.m_paintGunData)
    {
      this.RegisterPaintGun((ITxPaintGun) keyValuePair.Key);
      TxDisplayableObjectEx.SetSelectable((ITxDisplayableObject) keyValuePair.Key, false);
      this.ChangePaintBrush(keyValuePair.Value, keyValuePair.Key, keyValuePair.Value.BrushName);
    }
  }

  public void ClearUsingPaintSimulation()
  {
    foreach (KeyValuePair<TxPaintGun, CApPaintGun> keyValuePair in this.m_paintGunData)
    {
      this.UnRegisterPaintGun((ITxPaintGun) keyValuePair.Key);
      TxDisplayableObjectEx.SetSelectable((ITxDisplayableObject) keyValuePair.Key, true);
      keyValuePair.Value.DeleteBrush();
    }
    this.m_paintGunData.Clear();
  }

  private void ChangePaintBrush(CApPaintGun paintGun, TxPaintGun paintGunId, string brushName)
  {
    if (brushName == null)
      return;
    if (brushName != paintGun.BrushName)
    {
      paintGun.DeleteBrush();
      if (brushName.Length > 0)
        paintGun.BrushId = this.GetBrushSolid(paintGunId, brushName);
    }
    if (paintGun.BrushId != null)
    {
      paintGun.PaintGunIsOpenAndBrushNotDefinedNotification = false;
      paintGun.BrushName = brushName;
      if (paintGun.PaintGunIsOpen)
      {
        this.PaintGunManipulatorDisplay(paintGunId, false);
        paintGun.DisplayBrush(this.m_displayBrush);
      }
      else
        paintGun.DisplayBrush(false);
    }
    else
      paintGun.PaintGunIsOpenAndBrushNotDefinedNotification = true;
  }

  private void PaintGunManipulatorDisplay(TxPaintGun paintGun, bool display)
  {
    TxObjectList attachmentDescendants = ((ITxLocatableObject) paintGun.GunTip).GetDirectAttachmentDescendants((ITxTypeFilter) new TxTypeFilter(typeof (TxManipulator)));
    if (((Collection<ITxObject>) attachmentDescendants).Count != 1)
      return;
    if (display)
      (((Collection<ITxObject>) attachmentDescendants)[0] as ITxDisplayableObject).Display();
    else
      (((Collection<ITxObject>) attachmentDescendants)[0] as ITxDisplayableObject).Blank();
  }

  private ITxRobot GetMountedToRobot(TxPaintGun locatableObj)
  {
    TxFrame attachmentParent = locatableObj.AttachmentParent as TxFrame;
    if (TxFrame.op_Inequality(attachmentParent, (ITxObject) null))
    {
      ITxObject scope = TxEngineeringDataInternal.GetScope((ITxObject) attachmentParent);
      if (scope != null && scope is ITxRobot)
        return scope as ITxRobot;
    }
    return (ITxRobot) null;
  }

  private ITx2Or3DimensionalGeometry GetBrushSolid(TxPaintGun paintGun, string brushName)
  {
    ITx2Or3DimensionalGeometry brushSolid = (ITx2Or3DimensionalGeometry) null;
    TxPaintBrushManager paintBrushManager = TxApplicationEx.PaintManager().GetPaintBrushManager();
    TxRobot robot = (TxRobot) null;
    try
    {
      robot = this.GetMountedToRobot(paintGun) as TxRobot;
      if (TxRobot.op_Inequality(robot, (ITxObject) null))
      {
        TxPaintBrushData brushByName = paintBrushManager.GetBrushByName(robot, brushName);
        if (CApPaintDuringSimulationManager.Instance.UsePaintThickness)
        {
          if (!this.BrushGraphExist(brushName, paintGun, robot))
            goto label_11;
        }
        ITxComponent ghostBrush = paintBrushManager.CreateGhostBrush(robot, paintGun, brushByName.Id);
        if (ghostBrush != null)
        {
          if (this.m_displayBrush)
            ((ITxDisplayableObject) ghostBrush).Display();
          TxObjectList directDescendants = (ghostBrush as ITxObjectCollection).GetDirectDescendants((ITxTypeFilter) new TxTypeFilter(typeof (ITx2Or3DimensionalGeometry)));
          if (((Collection<ITxObject>) directDescendants).Count == 1)
            brushSolid = ((Collection<ITxObject>) directDescendants)[0] as ITx2Or3DimensionalGeometry;
        }
      }
    }
    catch (Exception ex)
    {
      if (this.m_paintSimulation == CApPaintDuringSimulationManager.SimulationState.SimulationForward)
        this.RaiseErrorMessageBrushNotExist(brushName, paintGun, robot);
    }
label_11:
    return brushSolid;
  }

  private bool BrushGraphExist(string brushName, TxPaintGun paintGun, TxRobot robot)
  {
    bool flag = TxApplicationEx.PaintManager().DoesPaintBrushGraphExist(brushName);
    if (!flag && this.m_paintSimulation == CApPaintDuringSimulationManager.SimulationState.SimulationForward)
      this.RaiseErrorMessageBrushGraphDoesNotExist(brushName, paintGun, robot);
    return flag;
  }

  private void RaiseErrorMessageGunOpenNoBrush(TxPaintGun paintGun)
  {
    string str1 = string.Format(DnProcessSimulateCommands.Paint.PaintDuringSimulation.Resources.StringTable.ERROR_MESSAGE_PAINT_GUN_IS_OPEN_NO_BRUSH, (object) ((TxBaseGeometryCreationObject) paintGun).Name);
    string str2 = " ";
    TxRobot mountedToRobot = this.GetMountedToRobot(paintGun) as TxRobot;
    if (TxRobot.op_Inequality(mountedToRobot, (ITxObject) null))
      str2 = ((TxBaseGeometryCreationObject) mountedToRobot).Name;
    string str3 = this.TimeValueToString(TxApplication.ActiveDocument.SimulationPlayer.CurrentTime);
    TxErrorStack.Push("PAINT_SIMULATION_ERROR", $"[{str2}][{str3}][Error]{str1}");
  }

  private void RaiseErrorMessageBrushNotExist(string brushName, TxPaintGun paintGun, TxRobot robot)
  {
    string str1 = string.Format(DnProcessSimulateCommands.Paint.PaintDuringSimulation.Resources.StringTable.ERROR_MESSAGE_BRUSH_NOT_EXIST, (object) brushName, (object) ((TxBaseGeometryCreationObject) paintGun).Name);
    string str2 = " ";
    if (TxRobot.op_Inequality(robot, (ITxObject) null))
      str2 = ((TxBaseGeometryCreationObject) robot).Name;
    string str3 = this.TimeValueToString(TxApplication.ActiveDocument.SimulationPlayer.CurrentTime);
    TxErrorStack.Push("PAINT_SIMULATION_ERROR", $"[{str2}][{str3}][Error]{str1}");
  }

  private void RaiseErrorMessageBrushGraphDoesNotExist(
    string brushName,
    TxPaintGun paintGun,
    TxRobot robot)
  {
    string str1 = string.Format(DnProcessSimulateCommands.Paint.PaintDuringSimulation.Resources.StringTable.ERROR_MESSAGE_BRUSH_GRAPH_DOES_NOT_EXIST, (object) brushName);
    string str2 = " ";
    if (TxRobot.op_Inequality(robot, (ITxObject) null))
      str2 = ((TxBaseGeometryCreationObject) robot).Name;
    string str3 = this.TimeValueToString(TxApplication.ActiveDocument.SimulationPlayer.CurrentTime);
    TxErrorStack.Push("PAINT_SIMULATION_ERROR", $"[{str2}][{str3}][Error]{str1}");
  }

  private string TimeValueToString(double val)
  {
    return this.DoubleToString(val / TxApplication.Options.Units.TimeMultiplier, "F" + TxApplication.Options.Units.TimeDigitsAfterDecimalPoint.ToString(), false);
  }

  private string DoubleToString(double val, string format, bool bTrimEndingZeros)
  {
    string str = val.ToString(format);
    if (NumberFormatInfo.CurrentInfo.NumberDecimalSeparator != ".")
      str = str.Replace(NumberFormatInfo.CurrentInfo.NumberDecimalSeparator, ".");
    if (bTrimEndingZeros)
    {
      if (str.Contains("."))
        str = str.TrimEnd('0');
      if (str.EndsWith("."))
        str = str.TrimEnd('.');
    }
    return str;
  }

  internal void Initialize()
  {
    if (this.m_initialized)
      return;
    this.m_initialized = true;
    CApPaintDisplayCoverageManager.Instance.LoadPaintSettingsFromRegistry();
    this.RegisterEvents();
  }

  private void RegisterEvents()
  {
    TxApplication.ApplicationEvents.Exiting += new TxApplication_ExitingEventHandler(this.ApplicationEvents_Exiting);
    TxApplication.DocumentCollection.DocumentLoaded += new TxDocumentCollection_DocumentLoadedEventHandler(this.DocumentCollection_DocumentLoaded);
    TxApplication.DocumentCollection.StudyModeChanged += new TxDocumentCollection_StudyModeChangedEventHandler(this.DocumentCollection_StudyModeChanged);
  }

  private void UnregisterEvents()
  {
    TxApplication.ApplicationEvents.Exiting -= new TxApplication_ExitingEventHandler(this.ApplicationEvents_Exiting);
    TxApplication.DocumentCollection.DocumentLoaded -= new TxDocumentCollection_DocumentLoadedEventHandler(this.DocumentCollection_DocumentLoaded);
    TxApplication.DocumentCollection.StudyModeChanged -= new TxDocumentCollection_StudyModeChangedEventHandler(this.DocumentCollection_StudyModeChanged);
  }

  private void SwitchOff()
  {
    CApPaintDuringSimulationManager.Instance.PaintDuringSimulation = false;
    CApPaintDuringSimulationManager.Instance.ClearUsingPaintSimulation();
  }

  private void ApplicationEvents_Exiting(object sender, TxApplication_ExitingEventArgs args)
  {
    this.UnregisterEvents();
  }

  private void DocumentCollection_DocumentLoaded(
    object sender,
    TxDocumentCollection_DocumentLoadedEventArgs args)
  {
    TxApplication.ActiveDocument.Unloading += new TxDocument_UnloadingEventHandler(this.ActiveDocument_Unloading);
  }

  private void ActiveDocument_Unloading(object sender, TxDocument_UnloadingEventArgs args)
  {
    TxApplication.ActiveDocument.Unloading -= new TxDocument_UnloadingEventHandler(this.ActiveDocument_Unloading);
    this.SwitchOff();
  }

  private void DocumentCollection_StudyModeChanged(
    object sender,
    TxDocumentCollection_StudyModeChangedEventArgs args)
  {
    TxApplication.DocumentCollection.StudyModeChanging += new TxDocumentCollection_StudyModeChangingEventHandler(this.DocumentCollection_StudyModeChanging);
  }

  private void DocumentCollection_StudyModeChanging(
    object sender,
    TxDocumentCollection_StudyModeChangingEventArgs args)
  {
    TxApplication.DocumentCollection.StudyModeChanging -= new TxDocumentCollection_StudyModeChangingEventHandler(this.DocumentCollection_StudyModeChanging);
    this.SwitchOff();
  }

  private enum SimulationState
  {
    NoSimulation,
    SimulationForward,
    SimulationStopForward,
    SimulationBackward,
    SimulationStopBackward,
  }
}
